<?php


namespace app\index\controller;


use app\common\model\Article;
use app\common\model\Carousel;
use app\common\model\Column;
use app\common\model\FriendLink;
use app\common\model\Tools;
use app\common\model\WebConfig;
use think\Exception;
use think\exception\HttpException;
use think\facade\View;

class Index extends Base
{
    public function index()
    {
        //轮播图
        $carouselList = Carousel::order('order_key asc')
            ->field('id,title,subtitle,img_path,text_align')
            ->cache('carousel_cache', 360)
            ->select();
        //小工具
        $tools = Tools::where(['status' => 1])
            ->field('id,title,icon,link')
            ->cache('tools_cache', 360)
            ->select();
        //获取最新的10条
        $option = [1, 10, [], ['columnInfo' => function ($query) {
            $query->field('id,title');
        }]];
        $articleList = Article::getArticleList(...$option);

        foreach ($articleList as $k => &$v) {
            $v['description'] = preg_replace('/\s+/', '', mb_substr(strip_tags(htmlspecialchars_decode($v['content'])), 0, 100)) . '......';
            unset($v['content']);
            $v['cover'] = explode(',', $v['cover']);
            $v['cover_count'] = count($v['cover']);
            $v['tag'] = explode(',', $v['tag']);
        }
        //统计数据
        $countList = Article::field("count(1) as total_num,sum(`like`) as like_num")
            ->cache('article_count_cache', 360)
            ->withCount(['comments'])
            ->find(true);
        $tag = Article::getTagList();
        $tagListCount = count($tag);

        //数据统计
        $countData = [
            ['title' => '文章', 'count' => $countList['total_num'] > 999 ? '999+' : $countList['total_num']],
            ['title' => '标签', 'count' => $tagListCount > 999 ? '999+' : $tagListCount],
            ['title' => '点赞', 'count' => $countList['like_num'] > 999 ? '999+' : $countList['like_num']],
            ['title' => '评论', 'count' => $countList['comments_count'] > 999 ? '999+' : $countList['comments_count']],
        ];


        unset($v);
        $readingCountRankingData = ['阅读排行', 'javascript:;', 'reading_count desc', 'cache_by_reading_count', 'id,title,reading_count as total'];
        $ranking = Article::getArticleRankingList(...$readingCountRankingData);
        //友情链接
        $friendlinkList = FriendLink::getFriendLink();
        $assignData = [
            'carousel_list' => $carouselList,
            'article_list'  => $articleList,
            'tools'         => $tools,
            'friend_link'   => $friendlinkList,
            'count_data'    => $countData,
            'link_apply'    => url('index_friend_link'),
            'web_notice'    => '欢迎光临傲杰笔记！',
            'ranking'       => $ranking,
            'tag_list'      => $tag,
        ];
        View::assign($assignData);

        return View::fetch();
    }

    public function friendLink()
    {

        if ($this->request->isPost()) {
            $params = $this->requestParams();
            if (!captcha_check($params['captcha'])) {
                $this->error('验证码错误');
            }
            $params['is_show'] = 0;
//            $pattern = "/http(s):\/\//";
//            $result = preg_replace($pattern, '', $params['link']);
//            $count = FriendLink::whereLike('link', '%' . $result . '%')->count();
            FriendLink::create($params);
            $this->success('友链申请成功，本站将会第一时间确认并为将您的链接添加至本站首页');
        }
        //友情链接
        $friendlinkList = FriendLink::getFriendLink();
        $assignData = [
            'friend_link' => $friendlinkList,
            'link_apply'  => (string)url('index_friend_link'),
        ];
        assign_config($assignData);
        View::assign($assignData);
        return View::fetch();
    }
}
